/**
 * 结束界面
 * 如果有与现有不一样的需求自己拓展实现
 */

import GameCtr from "../../Controller/GameCtr";
import WXCtr from "../../Controller/WXCtr";
import Http from "../../Common/Http";
import GameData from "../../Common/GameData";
import UserManager from "../../Common/UserManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class End extends cc.Component {

    @property(cc.Label)
    lbScore: cc.Label = null;
    @property(cc.Node)
    ndEnd: cc.Node = null;
    @property(cc.Sprite)
    sprOverScr: cc.Sprite = null;
    @property(cc.Node)
    ndSlider: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:

    private tex = null;
    private sliderData = null;
    private sliderIdx = 0;

    onLoad() {
        GameCtr.getInstance().setEnd(this);
    }

    start() {
        if (window.wx != undefined) {
            this.tex = new cc.Texture2D();
            window.sharedCanvas.width = 1080;
            window.sharedCanvas.height = 1200;
        }
         // GameCtr.getSliderConfig("settlement");
        if (GameCtr.reviveData != null && GameCtr.reviveData.background_switch && GameCtr.surplusReviveTimes > 0 && GameCtr.rankingEntrance == "Start") {
            // GameCtr.showRevive(true);
        }
        else {
            GameCtr.gameReviveData = [];
        }
        this.showScore(GameCtr.score);
        
        let datasocr = JSON.stringify(GameData.score);
        Http.send({
            url: Http.UrlConfig.SAVE_INFO,
            data:
                {
                    avatar_url: GameCtr.GameUerinfo.avatarUrl,
                    city: GameCtr.GameUerinfo.info.city,
                    country: GameCtr.GameUerinfo.country,
                    gender: GameCtr.GameUerinfo.GameUerinfofo.gender,
                    language: GameCtr.GameUerinfo.language,
                    nick_name: GameCtr.GameUerinfo.nickName,
                    openid: UserManager.user_id,
                    province: GameCtr.GameUerinfo.province,
                    datas:datasocr
                },
            success: () => {

            },
            error: () => {
                console.log("登陆失败", WXCtr.launchOption.query);
            }
        });
        // WXCtr.setBannerAd(WXCtr.advid);
        // this.schedule(() => {
        //     WXCtr.setBannerAd(WXCtr.advid);
        // }, 10);

    }

    //回到开始界面
    home() {
        GameCtr.showStart();
    }


    //重新开始（再来一局）
    reStart() {
        GameCtr.startGame();
    }

    //邀请好友
    invite() {
        WXCtr.share();
    }

    //显示分数
    showScore(score: number) {
        this.lbScore.string = score + "";
    }

    //显示排行榜
    showRankingView() {
        GameCtr.showRanking("End");
    }

    //显示广告按钮
    showSlider(slideData) {
        this.sliderData = slideData;
        this.ndSlider.active = true;
        this.setSlider();
    }

    setSlider() {
        if (!this.sliderData) return;
        console.log("end SliderData == ", this.sliderData);
        this.sliderIdx = Math.floor(Math.random() * this.sliderData.length);
        let data = this.sliderData[this.sliderIdx];
        let spr = this.ndSlider.getChildByName("btnSlider").getComponent(cc.Sprite);
        if (data.valid != undefined) {
            spr.node.active = data.valid == 1 ? true : false;
        }
        else {
            spr.node.active = false;
        }
        GameCtr.loadImg(spr, data.icon);
    }

    //点击广告按钮
    clickSlider(event, data) {
        let imgUrl = this.sliderData[this.sliderIdx].img;
        WXCtr.previewImg(imgUrl);
        this.setSlider();
    }

    onDestroy() {
        if (this.tex) {
            this.tex = null;
        }
        this.unscheduleAllCallbacks();
    }

    // 刷新子域的纹理
    _updateSubDomainCanvas() {
        if (window.sharedCanvas != undefined && this.tex != null && this.ndEnd.active) {
            this.tex.initWithElement(window.sharedCanvas);
            this.tex.handleLoadedTexture();
            this.sprOverScr.spriteFrame = new cc.SpriteFrame(this.tex);
        }
    }
    update() {
        this._updateSubDomainCanvas();
    }
}
